# link https://www.doubao.com/thread/w4e76f68188dfd6e5
import pygame
import random
import sys

# 初始化pygame
pygame.init()

# 确保中文正常显示
pygame.font.init()
font_path = pygame.font.match_font('simsun')  # 尝试匹配中文字体
if not font_path:
    # 如果找不到中文字体，使用默认字体
    font_path = pygame.font.get_default_font()
font = pygame.font.Font(font_path, 36)

# 游戏常量
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
FPS = 60
PLAYER_SIZE = 50
BALL_SIZE = 25
BOMB_SIZE = 30
PLAYER_SPEED = 8
FALL_SPEED = 5
SPAWN_RATE = 30  # 每隔多少帧生成一个新物体

# 颜色定义
WHITE = (255, 255, 255)
BLUE = (0, 0, 255)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
BLACK = (0, 0, 0)

# 创建屏幕
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("小球收集游戏")
clock = pygame.time.Clock()

class Player:
    def __init__(self):
        self.x = SCREEN_WIDTH // 2 - PLAYER_SIZE // 2
        self.y = SCREEN_HEIGHT - PLAYER_SIZE - 10
        self.width = PLAYER_SIZE
        self.height = PLAYER_SIZE
        self.speed = PLAYER_SPEED
        
    def move(self, direction):
        if direction == "left" and self.x > 0:
            self.x -= self.speed
        if direction == "right" and self.x < SCREEN_WIDTH - self.width:
            self.x += self.speed
            
    def draw(self):
        pygame.draw.rect(screen, BLUE, (self.x, self.y, self.width, self.height))
        
    def get_rect(self):
        return pygame.Rect(self.x, self.y, self.width, self.height)

class Ball:
    def __init__(self):
        self.x = random.randint(0, SCREEN_WIDTH - BALL_SIZE)
        self.y = -BALL_SIZE
        self.size = BALL_SIZE
        self.speed = FALL_SPEED
        self.type = random.choice(["good", "bad"])  # good是小球，bad是炸弹
        
    def fall(self):
        self.y += self.speed
        
    def draw(self):
        color = GREEN if self.type == "good" else RED
        pygame.draw.circle(screen, color, (self.x + self.size // 2, self.y + self.size // 2), self.size // 2)
        
    def is_off_screen(self):
        return self.y > SCREEN_HEIGHT
        
    def get_rect(self):
        return pygame.Rect(self.x, self.y, self.size, self.size)

def main():
    player = Player()
    balls = []
    score = 0
    frame_count = 0
    game_over = False
    # 在使用全局变量前声明
    global SPAWN_RATE
    
    while True:
        # 事件处理
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_r and game_over:
                    # 重新开始游戏
                    player = Player()
                    balls = []
                    score = 0
                    game_over = False
                    # 重置生成速度
                    SPAWN_RATE = 30
        
        if not game_over:
            # 生成新的小球或炸弹
            frame_count += 1
            if frame_count % SPAWN_RATE == 0:
                balls.append(Ball())
                # 随着分数增加，生成速度加快
                if score > 0 and score % 5 == 0:
                    SPAWN_RATE = max(10, SPAWN_RATE - 1)
            
            # 移动玩家
            keys = pygame.key.get_pressed()
            if keys[pygame.K_LEFT]:
                player.move("left")
            if keys[pygame.K_RIGHT]:
                player.move("right")
            
            # 移动小球和炸弹
            for ball in balls[:]:
                ball.fall()
                if ball.is_off_screen():
                    balls.remove(ball)
                    # 错过好球扣分
                    if ball.type == "good":
                        score = max(0, score - 1)
            
            # 碰撞检测
            player_rect = player.get_rect()
            for ball in balls[:]:
                ball_rect = ball.get_rect()
                if player_rect.colliderect(ball_rect):
                    balls.remove(ball)
                    if ball.type == "good":
                        score += 1
                    else:
                        # 碰到炸弹游戏结束
                        game_over = True
        
        # 绘制
        screen.fill(WHITE)
        player.draw()
        for ball in balls:
            ball.draw()
        
        # 显示分数
        score_text = font.render(f"分数: {score}", True, BLACK)
        screen.blit(score_text, (10, 10))
        
        # 游戏结束画面
        if game_over:
            game_over_text = font.render("游戏结束!", True, BLACK)
            restart_text = font.render("按 R 键重新开始", True, BLACK)
            screen.blit(game_over_text, (SCREEN_WIDTH//2 - 100, SCREEN_HEIGHT//2 - 50))
            screen.blit(restart_text, (SCREEN_WIDTH//2 - 150, SCREEN_HEIGHT//2 + 10))
        
        pygame.display.flip()
        clock.tick(FPS)

if __name__ == "__main__":
    main()
